package cn.shop.merchant.service;

import cn.shop.merchant.mapper.gametemplate.GameTemplateMapper;
import cn.shop.merchant.model.gameconfig.GameTemplate;
import cn.shop.merchant.req.GameTemplateQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import cn.shop.utils.enums.Status;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-1 16:11
 * @description : 游戏模板配置service
 */
@Service
@SuppressWarnings("unused")
@Transactional(rollbackFor = Exception.class)
public class GameTemplateService extends BaseServiceImpl<GameTemplateMapper, GameTemplate> {

    /**
     * 更新游戏模板
     * @param template 要更新or保存的对象
     * @return 是否成功
     */
    public boolean updateGameTemplate(GameTemplate template, long uid) {
        return update(template, Wrappers.<GameTemplate>lambdaQuery()
                .eq(GameTemplate::getUid, uid)
                .eq(GameTemplate::getId, template.getId()));
    }

    /**
     * 获取指定uid的分页列表
     *
     * @param uid 商户编号
     * @param req 查询条件
     * @return 查询到的集合
     */
    public Map<String, Object> findConfigList(long uid, GameTemplateQueryReq req) {
        Page<GameTemplate> page = Page.of(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<GameTemplate> query = Wrappers.<GameTemplate>lambdaQuery()
                .eq(GameTemplate::getUid, uid)
                .like(notBlank.test(req.getName()), GameTemplate::getName, req.getName())
                .eq(notNull.test(req.getStatus()), GameTemplate::getStatus, req.getStatus())
                .apply(notNull.test(req.getStartTime()), req.gtTime("create_time"), req.getStartTime())
                .apply(notNull.test(req.getEndTime()), req.ltTime("create_time"), req.getEndTime())
                .orderBy(true, isAsc.test(req.getOrderByTime()), GameTemplate::getCreateTime);
        page = page(page, query);
        return PageUtil.formatPage(page, this::format);
    }

    public List<Map<String, Object>> allList(long uid) {
        LambdaQueryWrapper<GameTemplate> query = Wrappers.<GameTemplate>lambdaQuery()
                .eq(GameTemplate::getUid, uid);
        return list(query).stream()
                .map(t -> Putif.start()
                        .anyway("value", t.getId())
                        .anyway("label", t.getName())
                        .get())
                .collect(Collectors.toList());
    }

    /**
     * 删除指定uid和id的记录
     * @param id 记录编号
     * @param uid 商户编号
     * @return 是否删除成功
     */
    public boolean delete(long id, long uid) {
        return SqlHelper.retBool(getBaseMapper().batchDeleteWithFill(new GameTemplate(), Wrappers.<GameTemplate>lambdaQuery()
                .eq(GameTemplate::getId, id)
                .eq(GameTemplate::getUid, uid)));
    }

    /**
     * 获取指定id和uid的记录
     * @param id 模板编号
     * @param uid 商户编号
     * @return 查询的信息
     */
    public Map<String, Object> getInfo(long id, long uid) {
        GameTemplate template = getOne(Wrappers.<GameTemplate>lambdaQuery()
                .eq(GameTemplate::getId, id)
                .eq(GameTemplate::getUid, uid));
        return format(template);
    }

    /**
     * 修改指定记录的状态
     *
     * @param ids 模板编号
     * @param uid 商户编号
     * @param status 状态
     * @return 是否修改成功
     */
    public boolean changeStatus(long[] ids, long uid, Status status) {
        List<Long> idList = Arrays.stream(ids).boxed().collect(Collectors.toList());
        GameTemplate update = new GameTemplate();
        update.setStatus(status);
        return update(update, Wrappers.<GameTemplate>lambdaUpdate()
                .in(GameTemplate::getId, idList)
                .eq(GameTemplate::getUid, uid));
    }

    /**
     * 格式化数据的方法
     * @param template 要格式化的数据
     * @return 格式化以后的数据
     */
    private Map<String, Object> format(GameTemplate template) {
        return Putif.start()
                .anyway("id", template.getId())
                .anyway("name", template.getName())
                .anyway("charge", template.getCharge())
                .anyway("rate", template.getRate())
                .anyway("status", template.getStatus())
                .anyway("createTime", template.getCreateTime())
                .anyway("gameCurrency", template.getGameCurrency())
                .anyway("engineName", template.getEngineName())
                .anyway("browserCommand", template.getBrowserCommand())
                .anyway("multiple", template.getMultiple())
                .get();
    }
}
